<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Gass_apply_model extends CI_Model {
	
	// The following method prevents an error occurring when $this->data is modified.
	// Error Message: 'Indirect modification of overloaded property Demo_cart_admin_model::$data has no effect'.
	public function &__get($key)
	{
		$CI =& get_instance();
		return $CI->$key;
	}
 	/**
	 * 获取所有学生的申请信息
	 *
	 * 研招办老师获取所有学生的申请信息，专业学院老师只能获取报考本学院的学生申请信息 
	 * 对于那些尚未提交的学生申请，不能在这个地方进行浏览。
	 * 专业学院老师只能看见已通过研招办老师审核之后的学生申请信息。
	 */	
	function get_apply_list()
	{
		$uri = $this->uri->uri_to_assoc(3);     
		$limit = $this->config->item('pag_limit');
		$offset = (isset($uri['page'])) ? $uri['page'] : FALSE;	
		
	  $is_college_teacher = FALSE; 
    if ( $this->flexi_auth->get_user_group_id() == 2 )
    {
     $current_user_array = $this->flexi_auth->get_user_by_id($this->flexi_auth->get_user_id())->row_array();
     $is_college_teacher = TRUE;
    }
    
		if (array_key_exists('search_key_words', $uri)){
			$pagination_url = 'gass_apply/manage_apply/search_key_words/'.$uri['search_key_words'].'/select_news_status/'.$uri['select_news_status'].'/select_college/'.$uri['select_college'].'/select_news_type/'.$uri['select_news_type'].'/';
			$config['uri_segment'] = 12; // Changing to 6 will select the 6th segment, example 'controller/function/search/query/page/10'.
	    
	    $search_key_words = str_replace('-',' ',urldecode($uri['search_key_words']));
	    $select_news_status = str_replace('-',' ',urldecode($uri['select_news_status']));
      $select_college = str_replace('-',' ',urldecode($uri['select_college']));
      $select_news_type = str_replace('-',' ',urldecode($uri['select_news_type']));
      
      $this->db->select('*');
      $this->db->from('applydetailsview');
      $where = '';
      
      if ($select_news_status!='all'){
      	$where = "apply_status = ".$select_news_status;
      }else{
      	if($is_college_teacher) 
      	 $where = "(apply_status > 2 and apply_status != 5)";
      	else
      	 $where = "apply_status != 1";
      }
      
      if ($search_key_words!='0000'){
      	$where = $where." and ( stu_real_name like '%".$search_key_words."%' or ungra_school like '%".$search_key_words."%' or ungra_major like '%".$search_key_words."%' or college_name like '%".$search_key_words."%' )";
      }
      
      if($is_college_teacher) $where = $where." and apply_college=".$current_user_array['upro_college_id'];     
           
      if ($select_college!='all')
      {
      	$where = $where." and apply_college =".$select_college;
      }           
      if ($select_news_type!='all')
      {
      	$where = $where." and apply_type =".$select_news_type;
      }           
      $this->db->where($where);
      $this->db->limit($limit, $offset);
      $query =$this->db->get();
      $this->db->select('*');
      $this->db->from('applydetailsview');    
      $this->db->where($where);
			$querynum = $this->db->get();
 			$total_public_apply = $querynum->num_rows();				
			$this->data['applyListInfo'] = $query->result_array();	
	  }else{	  
	  	$search_key_words	='';
	  	$select_news_status	='';
	  	$select_college	='';
	  	$select_news_type	='';
			// Set some defaults.
			$pagination_url = 'gass_apply/manage_apply/';
			$search_query = FALSE;
			$config['uri_segment'] = 4; // Changing to 4 will select the 4th segment, example 'controller/function/page/10'.
	          
      $this->db->select('*');
      $this->db->from('applydetailsview');
      
      $where = '';
      if($is_college_teacher) 
       $where = "apply_status > 2 and apply_status != 5 and apply_college = ".$current_user_array['upro_college_id'];
      else
       $where = "apply_status != 1";      
      $this->db->where($where);
      $this->db->limit($limit, $offset);
      $query =$this->db->get();
      $this->db->select('*');
      $this->db->from('applydetailsview');    
      $this->db->where($where);      
			$querynum = $this->db->get();
 			$total_public_apply = $querynum->num_rows();				
			$this->data['applyListInfo'] = $query->result_array();      
	  }
		// Create user record pagination.
		$this->load->library('pagination');	
		$config['base_url'] = base_url().$pagination_url.'page/';
		$config['total_rows'] = $total_public_apply;
		$config['per_page'] = $limit; 
		$this->pagination->initialize($config); 
		
		// Make search query and pagination data available to view.
		$this->data['search_key_words'] = ($search_key_words!='0000')?$search_key_words:''; // Populates search input field in view.
		$this->data['select_news_status'] = $select_news_status; 
		$this->data['select_college'] = $select_college; 
		$this->data['select_news_type'] = $select_news_type; 
		$this->data['pagination']['links'] = $this->pagination->create_links();
		$this->data['pagination']['total_public_apply'] = $total_public_apply;
	}
 	/**
	 * apply_details
	 * 根据申请表ID获取学生提交的申请信息详情
	 */	
	function apply_details($aId){
		return $this->db->get_where('applydetailsview',array('apply_id' => $aId))->row_array();
	}
 	/**
	 * authorized_apply
	 * 对学生申请进行审批
	 */		
	function authorized_apply($aId){
		$queryTemp = $this->db->get_where('ga_apply_info',array('apply_id' => $aId))->row_array();
    $data = array('apply_status' => ($queryTemp['apply_status']==2)?3:4);
    
    $this->db->where('apply_id', $aId);
    $this->db->update('ga_apply_info', $data); 	
    redirect('gass_apply/manage_apply');	
	}
	
 	/**
	 * 退回学生申请，并注明原因
	 *
	 * 根据不同的申请状态，退回之后的审批状态有区别
	 * 被研招办退回的状态区分于被二级单位退回的状态
	 */	
	function return_apply($aId)
	{
    $data = array(      
      'apply_feedback' => $this->input->post('back_reason_text')
    );		
		$apply_data_array = $this->db->get_where('ga_apply_info',array('apply_id' => $aId))->row_array();
		switch($apply_data_array['apply_status'])
		{
			case 2:
			 $data['apply_status'] = 5;
			 break;
			case 3:
			 $data['apply_status'] = 6;
			 break;
		}
    $this->db->where('apply_id', $aId);
    $this->db->update('ga_apply_info', $data); 	
    redirect('gass_apply/manage_apply');	
	}
}